# -*- coding: utf-8 -*-

import scrapy

from ScrapyObject.spiders.utils.url_utils import *
'''
需翻墙
scrapy crawl xsmdl -o xsmdl.json
https://douyin.wmdy10.fun/douyin/
'''


class XsmdlSpider(scrapy.Spider):
    # 前缀
    prefix = 'https://douyin.'
    # 中缀
    website = 'wmdy10'
    # 后缀
    suffix = '.fun/'
    name = 'xsmdl'
    allowed_domains = [website + '.fun']
    start_urls = ['https://douyin.wmdy10.fun/douyin/']

    def __init__(self):
        self.i = 0

    def parse(self, response):
        # 获取字符串类型的网页内容
        content = get_data(response)
        video_list = get_video_url_one(content)
        tag_list = response.xpath("//div[@class='data']/text()").extract()
        if len(video_list) and len(tag_list):
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags=tag_list[0].strip().split("/")[1].strip(), url=response.url, name='', pUrl="", vUrl=format_url_one(video_list[0]))
        url_list = response.xpath("//a[@class='stui-vodlist__thumb lazyload']/@href").extract()
        picture_list = response.xpath("//a[@class='stui-vodlist__thumb lazyload']/@data-original").extract()
        name_list = response.xpath("//a[@class='stui-vodlist__thumb lazyload']/@title").extract()
        if len(url_list) and len(name_list) and len(picture_list):
            for index, value in enumerate(url_list):
                self.i = self.i + 1
                yield get_video_item(id=self.i, url=split_joint(self.prefix + self.website + self.suffix, url_list[index]), name=name_list[index], pUrl=picture_list[index])
        # 从网页中提取url链接
        url_list = get_url(content)
        # 提取url
        for url in url_list:
            if url.startswith('/') or url.startswith('html'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
            elif url.startswith('http') or url.startswith('www'):
                yield scrapy.Request(url, callback=self.parse)
